from typing import Optional


class ExamRecord:
    """考试记录实体类"""
    def __init__(self,
                 id: Optional[int] = None,
                 category: Optional[str] = None,
                 exam_date: Optional[str] = None,
                 duration_seconds: Optional[float] = None,
                 user_id: Optional[int] = None):
        """
        初始化考试记录实体
        :param id: 考试ID
        :param category: 考试分类
        :param exam_date: 考试日期
        :param duration_seconds: 考试时长(秒)
        :param user_id: 用户ID
        """
        self.id = id
        self.category = category
        self.exam_date = exam_date
        self.duration_seconds = duration_seconds
        self.user_id = user_id

    def __dict__(self) -> dict:
        """将实体转换为字典"""
        return {
            'id': self.id,
            'category': self.category,
            'exam_date': self.exam_date,
            'duration_seconds': self.duration_seconds,
            'user_id': self.user_id
        }

    @classmethod
    def from_dict(cls, data: dict) -> 'ExamRecord':
        """从字典创建实体"""
        return cls(
            id=data.get('id'),
            category=data.get('category'),
            exam_date=data.get('exam_date'),
            duration_seconds=data.get('duration_seconds'),
            user_id=data.get('user_id')
        )